package com.kch.system.service.impl;

import com.kch.system.domain.Enterprise;
import com.kch.system.mapper.EnterpriseMapper;
import com.kch.system.service.EnterpriseService;
import com.kch.system.vo.EnterpriseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;


    @Override
    public int insertEnterprise(Enterprise enterprise) {
        enterprise.setStatus("0");
        enterprise.setCreateTime(new Date());
        return enterpriseMapper.insert(enterprise);
    }

    @Override
    public EnterpriseVO selectByUserId(Integer userId) {
        Enterprise enterprise = enterpriseMapper.selectByUserId(userId);
        EnterpriseVO enterpriseVO = new EnterpriseVO();
        if(!ObjectUtils.isEmpty(enterprise)){
            BeanUtils.copyProperties(enterprise, enterpriseVO);
            enterpriseVO.setImageurl(enterprise.getLogoImg());
        }
        return enterpriseVO;
    }

    @Override
    public EnterpriseVO selectVOById(Integer id) {
        Enterprise enterprise = enterpriseMapper.selectByPrimaryKey(id);
        EnterpriseVO enterpriseVO = new EnterpriseVO();
        if(!ObjectUtils.isEmpty(enterprise)){
            BeanUtils.copyProperties(enterprise, enterpriseVO);
            enterpriseVO.setImageurl(enterprise.getLogoImg());
        }
        return enterpriseVO;
    }

    @Override
    public Enterprise selectById(Integer id) {
        return enterpriseMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateById(Enterprise enterprise) {
        enterprise.setUpdateTime(new Date());
        return enterpriseMapper.updateByPrimaryKey(enterprise);
    }

    @Override
    public List<Enterprise> selectEnterprisePage(Enterprise enterprise) {
        return enterpriseMapper.selectEnterprisePage(enterprise);
    }

    @Override
    public boolean enterPriseIsPass(Integer userId) {
        Enterprise enterprise = enterpriseMapper.selectPassByUserId(userId);
        return ObjectUtils.isEmpty(enterprise);
    }

    @Override
    public Enterprise selectByUser(Integer userId) {
        return enterpriseMapper.selectByUserId(userId);
    }

    @Override
    public Enterprise selectPassByUserId(Integer userId) {
        return enterpriseMapper.selectPassByUserId(userId);
    }
}

